package com.workoutgrabber.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.workoutgrabber.ui.panels.WorkoutPanel;

public class WorkoutgrabberFrame extends JFrame implements ActionListener {
	private JDesktopPane desktop;
	private JInternalFrame frame1;
	private JMenuItem menuItemShowInternalFrame1, menuItemExit;
	private WorkoutPanel workoutPanel;
	
	public WorkoutgrabberFrame(WorkoutPanel workoutPanel){
		this.workoutPanel = workoutPanel;
		
		buildInterface();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == menuItemShowInternalFrame1)
			frame1.setVisible(true);
	
		if(e.getSource() == menuItemExit){
			this.dispose();
			System.exit(0);
		}	
	}	

	private void buildInterface() {
		desktop = new JDesktopPane();
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		setContentPane(desktop);
	
		frame1 = createInternalCreateWorkout();		
		desktop.add(frame1);
		frame1.setVisible(true);
		
		setJMenuBar(createMenuBar());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("TODO - some name");
		setSize(new Dimension(800, 600));
		setVisible(true);
	}
	
	private JInternalFrame createInternalCreateWorkout() {
		JInternalFrame internalFrame = new JInternalFrame("Create workout");
		internalFrame.setResizable(true);
		internalFrame.setIconifiable(true);
		internalFrame.setClosable(true);
		internalFrame.setDefaultCloseOperation(HIDE_ON_CLOSE);

		internalFrame.add(workoutPanel);
		internalFrame.pack();
		
		return internalFrame;
	}

	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		JMenu menuFile = new JMenu("File");
		menuItemShowInternalFrame1 = new JMenuItem("Create workout");
		menuItemExit = new JMenuItem("Exit");
		
		menuItemShowInternalFrame1.addActionListener(this);
		menuItemExit.addActionListener(this);
		
		menuFile.add(menuItemShowInternalFrame1);
		menuFile.add(menuItemExit);
		menuBar.add(menuFile);
		
		return menuBar;
	}
}
